<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item
        label="订单ID"
        prop="orderId"
      >
        <el-input
          v-model="dataForm.orderId"
          required 
          placeholder="订单ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="退货商品ID"
        prop="skuId"
      >
        <el-input
          v-model="dataForm.skuId"
          required 
          placeholder="退货商品ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="订单编号"
        prop="orderSn"
      >
        <el-input
          v-model="dataForm.orderSn"
          required 
          placeholder="订单编号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="会员用户名"
        prop="userUsername"
      >
        <el-input
          v-model="dataForm.userUsername"
          required 
          placeholder="会员用户名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="退款金额"
        prop="returnAmount"
      >
        <el-input
          v-model="dataForm.returnAmount"
          required 
          placeholder="退款金额"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="退货人姓名"
        prop="returnName"
      >
        <el-input
          v-model="dataForm.returnName"
          required 
          placeholder="退货人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="退货人电话"
        prop="returnPhone"
      >
        <el-input
          v-model="dataForm.returnPhone"
          required 
          placeholder="退货人电话"
        ></el-input>
      </el-form-item>

      <el-form-item 
        label="申请状态"
        prop="status"
      >
        <template>
          <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">待处理</el-radio>
          <el-radio :label="1">退货中</el-radio>
          <el-radio :label="2">已完成</el-radio>
          <el-radio :label="3">已拒绝</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item
        label="商品图片"
        prop="skuImg"
      >
        <single-upload v-model="dataForm.skuImg"></single-upload>
      </el-form-item>
      <el-form-item
        label="商品名称"
        prop="skuName"
      >
        <el-input
          v-model="dataForm.skuName"
          required 
          placeholder="商品名称"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="商品品牌"
        prop="skuBrand"
      >
        <el-input
          v-model="dataForm.skuBrand"
          required 
          placeholder="商品品牌"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="商品销售属性"
        prop="skuAttrVals"
      >
        <el-input
          v-model="dataForm.skuAttrVals"
          required 
          placeholder="商品销售属性"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="退货数量"
        prop="skuCount"
      >
        <el-input
          v-model="dataForm.skuCount"
          required 
          placeholder="退货数量"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="商品单价"
        prop="skuPrice"
      >
        <el-input
          v-model="dataForm.skuPrice"
          required 
          placeholder="商品单价"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="商品实付单价"
        prop="skuRealPrice"
      >
        <el-input
          v-model="dataForm.skuRealPrice"
          required 
          placeholder="商品实付单价"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="原因"
        prop="reason"
      >
        <template>
          <el-select v-model="dataForm.reason" placeholder="原因">
            <el-option
              v-for="item in reasonList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-form-item>

      <el-form-item
        label="描述"
        prop="description"
      >
        <el-input
          v-model="dataForm.description"
          required 
          placeholder="描述"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="凭证图片"
        prop="descPics"
      >
        <single-upload v-model="dataForm.descPics"></single-upload>
      </el-form-item>

      <el-form-item
        label="处理备注"
        prop="handleNote"
      >
        <el-input
          v-model="dataForm.handleNote"
          required 
          placeholder="处理备注"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="处理人员"
        prop="handleMan"
      >
        <el-input
          v-model="dataForm.handleMan"
          required 
          placeholder="处理人员"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="收货人"
        prop="receiveMan"
      >
        <el-input
          v-model="dataForm.receiveMan"
          required 
          placeholder="收货人"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="收货时间"
        prop="receiveTime"
      >
        <el-date-picker 
        v-model="dataForm.receiveTime" 
        type="date" 
        placeholder="请选择收货日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="收货备注"
        prop="receiveNote"
      >
        <el-input
          v-model="dataForm.receiveNote"
          required 
          placeholder="收货备注"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="收货电话"
        prop="receivePhoen"
      >
        <el-input
          v-model="dataForm.receivePhoen"
          required 
          placeholder="收货电话"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="公司收货地址"
        prop="companyAddress"
      >
        <el-input
          v-model="dataForm.companyAddress"
          required 
          placeholder="公司收货地址"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from './singleUpload'
export default {
  components: { SingleUpload },
  data () {
    return {
      visible: false,
      roleList: [],
      reasonList: [],
      dataForm: {
        id: 0,
        orderId: 0,
        skuId: 0,
        orderSn: '',
        userUsername: '',
        returnAmount: 0,
        returnName: '',
        returnPhone: '',
        status: 1,
        skuImg: '',
        skuName: '',
        skuBrand: '',
        skuAttrVals: '',
        skuCount: 0,
        skuPrice: 0,
        skuRealPrice: 0,
        reason: '',
        description: '',
        descPics: '',
        handleNote: '',
        handleMan: '',
        handleTime: '',
        receiveMan: '',
        receiveTime: '',
        receiveNote: '',
        receivePhoen: '',
        companyAddress: '',
        createTime: '',
        newTime: ''
      },
      dataRule: {
        orderId: [{ required: true, message: '订单ID不能为空', trigger: 'blur' }],
        skuId: [
          { required: true, message: '退货商品ID不能为空', trigger: 'blur' }
        ],
        orderSn: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' }
        ],
        userUsername: [
          { required: false, message: '会员用户名不能为空', trigger: 'blur' }
        ],
        returnAmount: [
          { required: false, message: '退款金额不能为空', trigger: 'blur' }
        ],
        returnName: [
          { required: false, message: '退货人姓名不能为空', trigger: 'blur' }
        ],
        returnPhone: [
          { required: true, message: '请输入退货人手机号', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: '请输入正确的手机号码或者座机号'
          }
        ],
        status: [
          {
            required: true,
            message: '申请状态[0->待处理；1->退货中；2->已完成；3->已拒绝]不能为空',
            trigger: 'blur'
          }
        ],
        skuImg: [
          { required: true, message: '商品图片不能为空', trigger: 'blur' }
        ],
        skuName: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        skuBrand: [
          { required: true, message: '商品品牌不能为空', trigger: 'blur' }
        ],
        skuAttrVals: [
          { required: true, message: '商品销售属性不能为空', trigger: 'blur' }
        ],
        skuCount: [
          { required: true, message: '退货数量不能为空', trigger: 'blur' }
        ],
        skuPrice: [
          { required: true, message: '商品单价不能为空', trigger: 'blur' }
        ],
        skuRealPrice: [
          { required: true, message: '商品实付单价不能为空', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '原因不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        descPics: [
          { required: true, message: '凭证图片不能为空', trigger: 'blur' }
        ],
        handleNote: [
          { required: true, message: '处理备注不能为空', trigger: 'blur' }
        ],
        handleMan: [
          { required: true, message: '处理人员不能为空', trigger: 'blur' }
        ],
        receiveMan: [
          { required: true, message: '收货人不能为空', trigger: 'blur' }
        ],
        receiveTime: [
          { required: true, message: '收货时间不能为空', trigger: 'blur' }
        ],
        receiveNote: [
          { required: true, message: '收货备注不能为空', trigger: 'blur' }
        ],
        receivePhoen: [
          { required: true, message: '请输入收货人手机号', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: '请输入正确的手机号码或者座机号'
          }
        ],
        companyAddress: [
          { required: true, message: '公司收货地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  activated () {
    this.getReasonList()
  },
  methods: {
    // getCurrentTime () {
    //   this.newTime = new Date()
    //   console.log(this.newTime)
    // },
    // getReasonList () {
    //   this.$http({
    //     url: this.$http.adornUrl('/order/orderreturnreason/list'),
    //     method: 'get',
    //     params: this.$http.adornParams({
    //       'page': this.pageIndex,
    //       'limit': this.pageSize
    //     })
    //   }).then(({ data }) => {
    //     if (data && data.code === 0) {
    //       this.reasonList = data.page.list
    //       this.totalPage = data.page.totalCount
    //       console.log('查到了')
    //       console.log(this.reasonList)
    //     } else {
    //       this.reasonList = []
    //       this.totalPage = 0
    //       console.log('没查到')
    //     }
    //   })
    // },
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/order/orderreturnapply/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.orderId = data.orderReturnApply.orderId
              this.dataForm.skuId = data.orderReturnApply.skuId
              this.dataForm.orderSn = data.orderReturnApply.orderSn
              this.dataForm.userUsername = data.orderReturnApply.userUsername
              this.dataForm.returnAmount = data.orderReturnApply.returnAmount
              this.dataForm.returnName = data.orderReturnApply.returnName
              this.dataForm.returnPhone = data.orderReturnApply.returnPhone
              this.dataForm.status = data.orderReturnApply.status
              this.dataForm.skuImg = data.orderReturnApply.skuImg
              this.dataForm.skuName = data.orderReturnApply.skuName
              this.dataForm.skuBrand = data.orderReturnApply.skuBrand
              this.dataForm.skuAttrVals = data.orderReturnApply.skuAttrVals
              this.dataForm.skuCount = data.orderReturnApply.skuCount
              this.dataForm.skuPrice = data.orderReturnApply.skuPrice
              this.dataForm.skuRealPrice = data.orderReturnApply.skuRealPrice
              this.dataForm.reason = data.orderReturnApply.reason
              this.dataForm.description = data.orderReturnApply.description
              this.dataForm.descPics = data.orderReturnApply.descPics
              this.dataForm.handleNote = data.orderReturnApply.handleNote
              this.dataForm.handleMan = data.orderReturnApply.handleMan
              this.dataForm.receiveMan = data.orderReturnApply.receiveMan
              this.dataForm.receiveTime = data.orderReturnApply.receiveTime
              this.dataForm.receiveNote = data.orderReturnApply.receiveNote
              this.dataForm.receivePhoen = data.orderReturnApply.receivePhoen
              this.dataForm.companyAddress = data.orderReturnApply.companyAddress
              this.dataForm.createTime = data.orderReturnApply.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      // this.$options.methods.getCurrentTime()
      this.newTime = new Date()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/order/orderreturnapply/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'orderId': this.dataForm.orderId,
              'skuId': this.dataForm.skuId,
              'orderSn': this.dataForm.orderSn,
              'userUsername': this.dataForm.userUsername,
              'returnAmount': this.dataForm.returnAmount,
              'returnName': this.dataForm.returnName,
              'returnPhone': this.dataForm.returnPhone,
              'status': this.dataForm.status,
              'skuImg': this.dataForm.skuImg,
              'skuName': this.dataForm.skuName,
              'skuBrand': this.dataForm.skuBrand,
              'skuAttrVals': this.dataForm.skuAttrVals,
              'skuCount': this.dataForm.skuCount,
              'skuPrice': this.dataForm.skuPrice,
              'skuRealPrice': this.dataForm.skuRealPrice,
              'reason': this.dataForm.reason,
              'description': this.dataForm.description,
              'descPics': this.dataForm.descPics,
              'handleNote': this.dataForm.handleNote,
              'handleMan': this.dataForm.handleMan,
              'receiveMan': this.dataForm.receiveMan,
              'receiveTime': this.dataForm.receiveTime,
              'receiveNote': this.dataForm.receiveNote,
              'receivePhoen': this.dataForm.receivePhoen,
              'companyAddress': this.dataForm.companyAddress,
              'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
